home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / udev.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  14KB  |  294 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import string
  7. import posixpath
  8. from curses.ascii import isprint
  9. from checkbox.lib.bit import get_bitmask, test_bit
  10. from checkbox.lib.cache import cache
  11. from checkbox.lib.dmi import Dmi, DmiNotAvailable
  12. from checkbox.lib.input import Input
  13. from checkbox.lib.pci import Pci
  14. from checkbox.lib.usb import Usb
  15. from checkbox.properties import String
  16. from checkbox.registry import Registry
  17. from checkbox.registries.command import CommandRegistry
  18. from checkbox.registries.link import LinkRegistry
  19. from checkbox.registries.map import MapRegistry
  20.  
  21. class DeviceRegistry(Registry):
  22.     
  23.     def __init__(self, environment, attributes):
  24.         super(DeviceRegistry, self).__init__()
  25.         self._environment = environment
  26.         self._attributes = attributes
  27.  
  28.     
  29.     def __str__(self):
  30.         strings = _[1]
  31.         return '\n'.join(strings)
  32.  
  33.     
  34.     def _get_bus(self):
  35.         sys_path = posixpath.join('/sys%s' % self._environment['DEVPATH'], 'subsystem')
  36.         if posixpath.islink(sys_path):
  37.             link = os.readlink(sys_path)
  38.             if '/' in link:
  39.                 return posixpath.basename(link)
  40.         
  41.  
  42.     
  43.     def _get_category(self):
  44.         if 'sys_vendor' in self._attributes:
  45.             return 'SYSTEM'
  46.         if self._get_product_id():
  47.             return 'OTHER'
  48.  
  49.     
  50.     def _get_driver(self):
  51.         if 'DRIVER' in self._environment:
  52.             return self._environment['DRIVER']
  53.         if 'ID_USB_DRIVER' in self._environment:
  54.             return self._environment['ID_USB_DRIVER']
  55.  
  56.     
  57.     def _get_path(self):
  58.         return self._environment.get('DEVPATH')
  59.  
  60.     
  61.     def _get_product_id(self):
  62.         if 'PCI_ID' in self._environment:
  63.             (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
  64.             return int(product_id, 16)
  65.         if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
  66.             (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
  67.             return int(product_id, 16)
  68.         for attribute in ('idProduct', 'model_id'):
  69.             if attribute in self._attributes:
  70.                 return int(self._attributes[attribute], 16)
  71.         
  72.  
  73.     
  74.     def _get_vendor_id(self):
  75.         if 'PCI_ID' in self._environment:
  76.             (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
  77.             return int(vendor_id, 16)
  78.         if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
  79.             (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
  80.             return int(vendor_id, 16)
  81.         if 'idVendor' in self._attributes:
  82.             return int(self._attributes['idVendor'], 16)
  83.         vendor_id_path = posixpath.join(self._get_path(), '../vendor_id')
  84.         if posixpath.exists(vendor_id_path):
  85.             vendor_id = open(vendor_id_path, 'r').read().strip()
  86.             return int(vendor_id, 16)
  87.  
  88.     
  89.     def _get_subproduct_id(self):
  90.         if 'PCI_SUBSYS_ID' in self._environment:
  91.             pci_subsys_id = self._environment['PCI_SUBSYS_ID']
  92.             (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
  93.             return int(subproduct_id, 16)
  94.  
  95.     
  96.     def _get_subvendor_id(self):
  97.         if 'PCI_SUBSYS_ID' in self._environment:
  98.             pci_subsys_id = self._environment['PCI_SUBSYS_ID']
  99.             (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
  100.             return int(subvendor_id, 16)
  101.  
  102.     
  103.     def _get_product(self):
  104.         for element in ('NAME', 'RFKILL_NAME', 'POWER_SUPPLY_MODEL_NAME'):
  105.             if element in self._environment:
  106.                 return self._environment[element].strip('"')
  107.         
  108.         for attribute in ('description', 'model_name_kv', 'model', 'product_name'):
  109.             if attribute in self._attributes:
  110.                 return self._attributes[attribute]
  111.         
  112.         bus = self._get_bus()
  113.         if bus == 'sound':
  114.             device = posixpath.basename(self._environment['DEVPATH'])
  115.             match = re.match('(card|controlC|hwC|midiC)(?P<card>\\d+)', device)
  116.             if match:
  117.                 card = match.group('card')
  118.                 in_card = False
  119.                 file = open('/proc/asound/cards', 'r')
  120.                 for line in file.readlines():
  121.                     line = line.strip()
  122.                     match = re.match('(?P<card>\\d+) \\[', line)
  123.                     if in_card:
  124.                         match = re.match('(?P<name>.*) at (?P<address>0x[%s]{8}) irq (?P<irq>\\d+)' % string.hexdigits, line)
  125.                         if match:
  126.                             return match.group('name')
  127.                         continue
  128.                     match
  129.                 
  130.             
  131.             path = None
  132.             match = re.match('pcmC(?P<card>\\d+)D(?P<device>\\d+)(?P<type>\\w)', device)
  133.             if match:
  134.                 path = '/proc/asound/card%s/pcm%s%c/info' % match.groups()
  135.             
  136.             match = re.match('(dsp|adsp|midi|amidi|audio|mixer)(?P<card>\\d+)?', device)
  137.             if match:
  138.                 if not match.group('card'):
  139.                     pass
  140.                 card = 0
  141.                 path = '/proc/asound/card%s/pcm0p/info' % card
  142.             
  143.             if path and posixpath.exists(path):
  144.                 file = open(path, 'r')
  145.                 for line in file.readlines():
  146.                     match = re.match('name: (?P<name>.*)', line)
  147.                     if match:
  148.                         return match.group('name')
  149.                 
  150.             
  151.         
  152.  
  153.     
  154.     def _get_vendor(self):
  155.         if 'RFKILL_NAME' in self._environment:
  156.             return None
  157.         if 'POWER_SUPPLY_MANUFACTURER' in self._environment:
  158.             return self._environment['POWER_SUPPLY_MANUFACTURER']
  159.         if 'sys_vendor' in self._attributes:
  160.             return self._attributes['sys_vendor']
  161.         vendor_path = posixpath.join(self._get_path(), '../vendor_oui')
  162.         if posixpath.exists(vendor_path):
  163.             return open(vendor_path, 'r').read().strip()
  164.  
  165.     
  166.     def items(self):
  167.         return (('path', self._get_path()), ('bus', self._get_bus()), ('category', self._get_category()), ('driver', self._get_driver()), ('product_id', self._get_product_id()), ('vendor_id', self._get_vendor_id()), ('subproduct_id', self._get_subproduct_id()), ('subvendor_id', self._get_subvendor_id()), ('product', self._get_product()), ('vendor', self._get_vendor()), ('attributes', MapRegistry(self._attributes)), ('environment', MapRegistry(self._environment)), ('device', LinkRegistry(self)))
  168.  
  169.  
  170.  
  171. class DmiDeviceRegistry(DeviceRegistry):
  172.     
  173.     def __init__(self, environment, attributes, category):
  174.         super(DmiDeviceRegistry, self).__init__(environment, attributes)
  175.         self._category = category
  176.  
  177.     
  178.     def _get_category(self):
  179.         return self._category
  180.  
  181.     
  182.     def _get_path(self):
  183.         path = super(DmiDeviceRegistry, self)._get_path()
  184.         return posixpath.join(path, self._category.lower())
  185.  
  186.     
  187.     def _get_product(self):
  188.         if self._category == 'CHASSIS':
  189.             type = int(self._attributes['chassis_type'])
  190.             return Dmi.chassis_names[type]
  191.         for name in ('name', 'version'):
  192.             attribute = '%s_%s' % (self._category.lower(), name)
  193.             product = self._attributes.get(attribute)
  194.             if product and product != 'Not Available':
  195.                 return product
  196.         
  197.  
  198.     
  199.     def _get_vendor(self):
  200.         attribute = '%s_vendor' % self._category.lower()
  201.         if attribute in self._attributes:
  202.             return self._attributes[attribute]
  203.  
  204.     _get_vendor = DmiNotAvailable(_get_vendor)
  205.  
  206.  
  207. class UdevRegistry(CommandRegistry):
  208.     '''Registry for udev information.'''
  209.     command = String(default = 'udevadm info --export-db')
  210.     
  211.     def _get_attributes(self, path):
  212.         attributes = { }
  213.         sys_path = '/sys%s' % path
  214.         
  215.         try:
  216.             names = os.listdir(sys_path)
  217.         except OSError:
  218.             return attributes
  219.  
  220.         for name in names:
  221.             name_path = posixpath.join(sys_path, name)
  222.             if name[0] == '.' and name in ('dev', 'uevent') and posixpath.isdir(name_path) or posixpath.islink(name_path):
  223.                 continue
  224.             
  225.             
  226.             try:
  227.                 value = open(name_path, 'r').read().strip()
  228.             except IOError:
  229.                 continue
  230.  
  231.             value = value.split('\n')[0]
  232.             attributes[name] = value
  233.         
  234.         return attributes
  235.  
  236.     
  237.     def _ignore_device(self, device):
  238.         if not device.bus:
  239.             return True
  240.         if not (device.product) and device.product_id is None:
  241.             return True
  242.         if (device.subproduct_id is None or device.subvendor_id is not None or device.subproduct_id is not None) and device.subvendor_id is None:
  243.             return True
  244.         if device.bus != 'dmi' and 'virtual' in device.path.split(posixpath.sep):
  245.             return True
  246.         return False
  247.  
  248.     
  249.     def items(self):
  250.         devices = []
  251.         line_pattern = re.compile('(?P<key>\\w):\\s*(?P<value>.*)')
  252.         multi_pattern = re.compile('(?P<key>\\w+)=(?P<value>.*)')
  253.         for record in str(self).split('\n\n'):
  254.             if not record:
  255.                 continue
  256.             
  257.             path = None
  258.             environment = { }
  259.             for line in record.split('\n'):
  260.                 match = line_pattern.match(line)
  261.                 if not match:
  262.                     raise Exception, 'Device line not supported: %s' % line
  263.                 match
  264.                 key = match.group('key')
  265.                 value = match.group('value')
  266.                 if key == 'P':
  267.                     path = value
  268.                     continue
  269.                 if key == 'E':
  270.                     match = multi_pattern.match(value)
  271.                     if not match:
  272.                         raise Exception, 'Device property not supported: %s' % value
  273.                     match
  274.                     environment[match.group('key')] = match.group('value')
  275.                     continue
  276.             
  277.             environment.setdefault('DEVPATH', path)
  278.             attributes = self._get_attributes(path)
  279.             if path == '/devices/virtual/dmi/id':
  280.                 device = DeviceRegistry(environment, attributes)
  281.                 devices.append(device)
  282.                 for category in ('BIOS', 'BOARD', 'CHASSIS'):
  283.                     device = DmiDeviceRegistry(environment, attributes, category)
  284.                     devices.append(device)
  285.                 
  286.             device = DeviceRegistry(environment, attributes)
  287.             devices.append(device)
  288.         
  289.         return _[1]
  290.  
  291.     items = cache(items)
  292.  
  293. factory = UdevRegistry
  294.